/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.policy;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.createUniqueName;
import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getOwnedBehaviors;

import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.uml2.uml.Lifeline;

import com.cea.papyrus.diagram.sequence.sedi.edit.command.SelectLifelinePartCommand;
import com.cea.papyrus.diagram.sequence.sedi.edit.handlers.LifelineRenameHandler;
import com.cea.papyrus.diagram.sequence.sedi.figures.LifelineFigure;
import com.cea.papyrus.diagram.sequence.sedi.model.command.LifelineDecomposeCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * This edit policy is installed on lifeline and enables them to understand the
 * "DecomposeLifelineRequest".
 * 
 * @author frankd@ifi.uio.no
 */
public class LifelineEditPolicy extends LabelDirectEditPolicy {

    public Command getCommand(Request request) {
        if ("no.uio.ifi.uml.sedi.DecomposeLifelineRequest" == request.getType())
            return getLifelineDecomposeCommand(request);
        return super.getCommand(request);
    }

    @SuppressWarnings("unchecked")
    private Lifeline getLifeline() {
        final GraphElement lifelineView = (GraphElement) getHost().getModel();
        return ModelUtil.<Lifeline> getSemantic(lifelineView);
    }

    @Override
    protected Command getDirectEditCommand(final DirectEditRequest request) {
        final int target = getEditTarget(request);
        if (target == LifelineFigure.TARGET_REF)
            return getRenameDecompositionCommand(request);
        else
            return getRenameLifelineCommand(request);
    }

    protected Command getLifelineDecomposeCommand(Request request) {
        final Lifeline lifeline = getLifeline();
        final boolean decompose =
                ((Boolean) request.getExtendedData().get("decompose")).booleanValue();
        final String newText =
                decompose ? createUniqueName(getOwnedBehaviors(lifeline.getInteraction()
                        .getContext()), "Interaction_") : null;

        final CompoundCommand cmd = new CompoundCommand();
        cmd.add(new LifelineDecomposeCommand(lifeline, newText));
        if (decompose) {
            SelectLifelinePartCommand selCmd = new SelectLifelinePartCommand();
            selCmd.setAnyEditPart(getHost().getRoot());
            selCmd.setTarget(lifeline);
            selCmd.setSelectForEdit(true);
            selCmd.setSelectDecomposition(true);
            cmd.add(selCmd);
        }
        return cmd;
    }

    protected Command getRenameDecompositionCommand(final DirectEditRequest request) {
        final String text = (String) request.getCellEditor().getValue();
        return new LifelineDecomposeCommand(getLifeline(), text);
    }

    protected Command getRenameLifelineCommand(final DirectEditRequest request) {
        return new LifelineRenameHandler(this).getRenameCommands(request);
    }

    @Override
    protected void showCurrentEditValue(DirectEditRequest request) {
        String text = (String) request.getCellEditor().getValue();
        GraphicalEditPart host = (GraphicalEditPart) getHost();
        LifelineFigure figure = (LifelineFigure) host.getFigure();

        final int target = getEditTarget(request);
        if (target == LifelineFigure.TARGET_REF)
            figure.setRefText(text);
        else
            figure.setLabel(text);

        figure.getUpdateManager().performUpdate(); // TODO Can this line be removed?
    }

    private int getEditTarget(final DirectEditRequest request) {
        return ((Integer) request.getExtendedData().get("target")).intValue();
    }
}
